#!/usr/bin/python
#Auther : MillionLee

'''
Do update with oauth mode

do post after authed
weibo sample referenced, and as the baseclass of all the other weibo ops classes for now 
'''

import unittest
from weibopy.auth import OAuthHandler
from weibopy.api import API

from config import *

class OAuthUpdate(unittest.TestCase):
    
    consumer_key = OAUTH_CONSUMER_KEY_WEIBO
    consumer_secret = OAUTH_CONSUMER_SECRET_WEIBO
    
    def __init__(self):
            """ constructor """
    
    def getAtt(self, key):
        try:
            return self.obj.__getattribute__(key)
        except Exception, e:
            print e
            return ''
        
    def getAttValue(self, obj, key):
        try:
            return obj.__getattribute__(key)
        except Exception, e:
            print e
            return ''

    def setToken(self, token, tokenSecret):
        self.auth = OAuthHandler(self.consumer_key, self.consumer_secret)
        self.auth.setToken(token, tokenSecret)
        self.api = API(self.auth)
    
    def update(self, message):
        message = message.encode("utf-8")
        #status = self.api.update_status(status=message, lat="30", long="110")
        status = self.api.update_status(message)
        self.obj = status
        id = self.getAtt("id")
        text = self.getAtt("text")
        print "update---"+ str(id) +":"+ text
        
    def destroy_status(self, id):
        status = self.api.destroy_status(id)
        self.obj = status
        id = self.getAtt("id")
        text = self.getAtt("text")
        print "update---"+ str(id) +":"+ text
        
    def getUsername(self):
        return self.auth.get_username()